﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using AspNetCs.Config;
namespace AspNetCs.SQLServerDAL
{
    public class Pager : IPager
    {

        #region IPager 成员






        public void ControlBind(Control obj, string TableName, string ReColumns, string orderFld, int PageIndex, int PageSize, string OrderType, string WhereStr, string ident)
        {

            db Conn = new db();
            db.ParameterArry[] Wang_Parameter = new db.ParameterArry[9];
            Wang_Parameter[0].dbType = SqlDbType.VarChar; Wang_Parameter[0].objValue = TableName; Wang_Parameter[0].parName = "@tblName";
            Wang_Parameter[1].dbType = SqlDbType.VarChar; Wang_Parameter[1].objValue = ReColumns; Wang_Parameter[1].parName = "@RetColumns";
            Wang_Parameter[2].dbType = SqlDbType.VarChar; Wang_Parameter[2].objValue = orderFld; Wang_Parameter[2].parName = "@Orderfld";
            Wang_Parameter[3].dbType = SqlDbType.Int; Wang_Parameter[3].objValue = PageSize; Wang_Parameter[3].parName = "@PageSize";
            Wang_Parameter[4].dbType = SqlDbType.Int; Wang_Parameter[4].objValue = PageIndex; Wang_Parameter[4].parName = "@PageIndex";
            Wang_Parameter[5].dbType = SqlDbType.Bit; Wang_Parameter[5].objValue = 0; Wang_Parameter[5].parName = "@IsCount";
            Wang_Parameter[6].dbType = SqlDbType.VarChar; Wang_Parameter[6].objValue = OrderType; Wang_Parameter[6].parName = "@OrderType";
            Wang_Parameter[7].dbType = SqlDbType.VarChar; Wang_Parameter[7].objValue = WhereStr; Wang_Parameter[7].parName = "@strWhere";
            Wang_Parameter[8].dbType = SqlDbType.VarChar; Wang_Parameter[8].objValue = ident; Wang_Parameter[8].parName = "@ident";

            DataTable dt = Conn.RunDataSetParameter("GetRecordFromPage", Wang_Parameter, "PagerList");
            if (obj.GetType() == typeof(GridView))
            {
                GridView control = (GridView)obj;
                control.DataSource = dt;
                control.DataBind();
            }

            else if (obj.GetType() == typeof(DataList))
            {
                DataList control = (DataList)obj;
                control.DataSource = dt;
                control.DataBind();
            }
            else if (obj.GetType() == typeof(Repeater))
            {
                Repeater control = (Repeater)obj;
                control.DataSource = dt;
                control.DataBind();
            }
            else if (obj.GetType() == typeof(FormView))
            {
                FormView control = (FormView)obj;
                control.DataSource = dt;
                control.DataBind();
            }
            else if (obj.GetType() == typeof(DataGrid))
            {

                DataGrid control = (DataGrid)obj;
                control.DataSource = dt;
                control.DataBind();
            }
            else if (obj.GetType() == typeof(DetailsView))
            {

                DetailsView control = (DetailsView)obj;
                control.DataSource = dt;
                control.DataBind();
            }


        }

        public int ReCount(Control obj, string TableName, string ReColumns, string orderFld, int PageIndex, int PageSize, string OrderType, string WhereStr, string ident)
        {
            int Count = 0;
            try
            {
                db Conn = new db();
                db.ParameterArry[] Wang_Parameter = new db.ParameterArry[9];
                Wang_Parameter[0].dbType = SqlDbType.VarChar; Wang_Parameter[0].objValue = TableName; Wang_Parameter[0].parName = "@tblName";
                Wang_Parameter[1].dbType = SqlDbType.VarChar; Wang_Parameter[1].objValue = ReColumns; Wang_Parameter[1].parName = "@RetColumns";
                Wang_Parameter[2].dbType = SqlDbType.VarChar; Wang_Parameter[2].objValue = orderFld; Wang_Parameter[2].parName = "@Orderfld";
                Wang_Parameter[3].dbType = SqlDbType.Int; Wang_Parameter[3].objValue = PageSize; Wang_Parameter[3].parName = "@PageSize";
                Wang_Parameter[4].dbType = SqlDbType.Int; Wang_Parameter[4].objValue = PageIndex; Wang_Parameter[4].parName = "@PageIndex";
                Wang_Parameter[5].dbType = SqlDbType.Int; Wang_Parameter[5].objValue = 1; Wang_Parameter[5].parName = "@IsCount";
                Wang_Parameter[6].dbType = SqlDbType.VarChar; Wang_Parameter[6].objValue = OrderType; Wang_Parameter[6].parName = "@OrderType";
                Wang_Parameter[7].dbType = SqlDbType.VarChar; Wang_Parameter[7].objValue = WhereStr; Wang_Parameter[7].parName = "@strWhere";
                Wang_Parameter[8].dbType = SqlDbType.VarChar; Wang_Parameter[8].objValue = ident; Wang_Parameter[8].parName = "@ident";

                DataTable dt = Conn.RunDataSetParameter("GetRecordFromPage", Wang_Parameter, "PagerList");
                Count = Convert.ToInt32(dt.Rows[0][0]);
            }
            catch (Exception Ex)
            {
                new CustomException(Ex);
            }
            return Count;

        }

        #endregion
    }
}
